package com.fs;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;



public class Util {
	
	public static String dbConnectionString;
	public static String dbUser;
	public static String dbPassword;
	public static String fileName;
	
	public static void init(ArrayList<String> bans) {
		File initFile = new File("init.properties");
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader(initFile));
			String line = ""; 
			while ((line = reader.readLine()) != null) {
				if (line.startsWith("DB_CONNECTION_STRING")) {
					dbConnectionString = line.substring(line.indexOf("=") + 1);
				} else if (line.startsWith("DB_USER")) {
					dbUser = line.substring(line.indexOf("=") + 1);
				} else if (line.startsWith("DB_PASSWORD")) {
					dbPassword = line.substring(line.indexOf("=") + 1);
				} else if (line.startsWith("FILE")) {
					fileName = line.substring(line.indexOf("=") + 1);
				} else if (line.startsWith("BAN:")) {
					String ban = line.substring(line.indexOf(":") + 1);
					bans.add(ban);
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null)  {
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static String report(ArrayList<FinalResult> results) {
		Date now = new Date(System.currentTimeMillis());
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd_HH.mm.ss");//"dd/MM/yyyy"
		File reportDir = new File("reports" + File.separator + df.format(now));
		reportDir.mkdirs();
		boolean init = true;
		for (FinalResult result:results) {
			createDetailedReport(result, reportDir, init, false);
			init = false;
		}
		createDetailedReport(null, reportDir, false, true);
		return reportDir.getAbsolutePath() + File.separator + "index.html";
	}

	private static void createDetailedReport(FinalResult result, File reportDir, boolean init, boolean end) {
		
		
		FileWriter fw = null;
		FileWriter fwIndex = null;
		try {
			
			fwIndex = new FileWriter(new File(reportDir, "index.html"), true);
			if (end) {
				fwIndex.write("</table></body></html>");
				return;
			}
			if (init) {
				fwIndex.write("<html><head><title>Invoices Report " + reportDir.getName() + "</title></head><body bgcolor=#999999><table width=80% ALIGN=center>");
				fwIndex.write("<tr bgcolor=#999999 align=center>" +
						"<td><font face=\"Verdana\" size=5> <b>BAN</b></font></td>" +
						"<td><font face=\"Verdana\" size=5> <b>Invoice</b></font></td>" +
						"<td><font face=\"Verdana\" size=5> <b>Details</b></font></td></tr>");
			}
			File report = new File(reportDir,  result.getInvoice()[0] + ".html");
			String color = "#00FF00";
			if (!result.getFinalResult()) {
				color = "#FF0000";
			}
			fwIndex.write("<tr bgcolor=" + color + ">" +
					"<td><font face=\"Arial\" size=4>" + result.getInvoice()[1] + "</font></td>" +
					"<td><font face=\"Arial\" size=4>" + result.getInvoice()[0] + "</font></td>" +
					"<td><font face=\"Arial\" size=4><a href='" + report.getName() + "'>Details...</a></font></td></tr>");
			fwIndex.flush();
			fw = new FileWriter(report);
			fw.write("<html><head><title>Invoice " + result.getInvoice()[0] + "</title></head><body bgcolor=#999999><table width=80% ALIGN=center>");
			fw.write("<tr bgcolor=#999999 align=center>" +
					"<td><font face=\"Verdana\" size=5> <b>Invoice details</b></font></td></tr>");
			for (Result res:result.getResult()) {
				color = "#00FF00";
				if (!res.isOk()) {
					color = "#FF0000";
				}
				fw.write("<tr bgcolor=" + color + " align=left>" +
				"<td><font face=\"Arial\" size=4> " + res.getDescription() + "</font></td></tr>");
			}
			fw.write("</table><h3><a href='index.html'>...Back  to index</a></h3></body></html>");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fw != null) {
				try {
					fw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fwIndex != null) {
				try {
					fwIndex.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	@SuppressWarnings("unused")
	private static void createDetailedReportNoHtml(FinalResult result, File reportDir) {
		
		File report = new File(reportDir,  result.getInvoice()[0] + ".txt");
		
		FileWriter fw = null;
		FileWriter fwIndex = null;
		try {
			fw = new FileWriter(report);
			fwIndex = new FileWriter(new File(reportDir, "index.txt"), true);
			fwIndex.write("BAN: " + result.getInvoice()[1] + " Invoice: " + result.getInvoice()[0] + " is tested with status: " + result.getFinalResult() + "\n");
			fwIndex.flush();
			fw.write("BAN: " + result.getInvoice()[1] + " Invoice: " + result.getInvoice()[0] + " is tested with status: " + result.getFinalResult() + "\n");
			for (Result res:result.getResult()) {
				fw.write(res.toString());
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fw != null) {
				try {
					fw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fwIndex != null) {
				try {
					fwIndex.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
}
